package service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import model.Datentraeger;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * Class with the implementation of the custom repository code. Uses JDBC in
 * this case. For basic programatic setup see {@link DatentraegerRepositoryImpl}
 * for examples.
 * <p>
 * As you need to hand the instance a {@link javax.sql.DataSource} or
 * {@link org.springframework.jdbc.core.simple.SimpleJdbcTemplate} you manually
 * need to declare it as Spring bean:
 * 
 * <pre>
 * &lt;jpa:repository base-package=&quot;com.acme.repository&quot; /&gt;
 * 
 * &lt;bean id=&quot;datentraegerRepositoryImpl&quot; class=&quot;...DatentraegerRepositoryJdbcImpl&quot;&gt;
 *   &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; /&gt;
 * &lt;/bean&gt;
 * </pre>
 * 
 * Using {@code datentraegerRepositoryImpl} will cause the repository instance
 * get this bean injected for custom repository logic as the default postfix for
 * custom DAO instances is {@code Impl}.
 * 
 * @author Andre
 */
class DatentraegerRepositoryJdbcImpl extends JdbcDaoSupport implements
		DatentraegerRepositoryFilmVerwaltung {

	private static final String COMPLICATED_SQL = "SELECT * FROM Datentraeger";

	/*
	 * (non-Javadoc)
	 * 
	 * @see sevice.impl.DatentraegerRepositoryFilmVerwaltung#
	 * filmVerwaltungBatchOperation()
	 */
	public List<Datentraeger> filmVerwaltungBatchOperation() {

		return getJdbcTemplate().query(COMPLICATED_SQL,
				new DatentraegerRowMapper());
	}

	private static class DatentraegerRowMapper implements
			ParameterizedRowMapper<Datentraeger> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.springframework.jdbc.core.simple.ParameterizedRowMapper#mapRow
		 * (java.sql.ResultSet, int)
		 */
		public Datentraeger mapRow(ResultSet rs, int rowNum)
				throws SQLException {

			Datentraeger datentraeger = new Datentraeger(rs.getString("name"));
			datentraeger.setName(rs.getString("name"));

			return datentraeger;
		}
	}
}
